package edu.ccut.saturn.studio.export.model;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Document;

import edu.ccut.saturn.studio.core.exception.XMLException;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.saturndata.model.ValidateModel;
import edu.ccut.saturn.studio.saturndata.model.ValidateVO;

/**
 * 生成验证构件的具体实现类
 * 
 * @author 杨艳斌
 * 
 * @version 1.1 2009.12.11
 */

public class Validate {
	
	private static String filename;
	private static String info;
	
	/**
	 * 创建验证构件的html文档
	 * 
	 * @param file,filePath
	 * @return String
	 * @throws XMLException
	 */
	public static String createComponentHtml(File file,String filePath) throws XMLException{
		
		filename = file.getName().replace(".", "-");
		Document doc = SaturnW3CUtil.getDocument(file.getAbsolutePath());
		
		ValidateModel validate = new ValidateModel(doc);
		ValidateVO validateNode = validate.getValidateVO();
		generateInfo(validateNode, filePath);
		
		return info;
	}
	
	/**
	 * 创建验证构件的html文档的具体实现方法
	 * 
	 * @param file,filePath
	 * @return String
	 * @throws XMLException
	 */
	private static void generateInfo(ValidateVO validateNode, String filePath) {
		Map<String, List<?>> parameters = new HashMap<String, List<?>>();
		List<Parameters> basicvaluse = new ArrayList<Parameters>();
		String tempalatePath = "template/export/validate";
		Parameters para = new Parameters();
		info = validateNode.getDescription();
		
		para.setName(validateNode.getName());
		para.setKey(validateNode.getKey());
		para.setClassname(validateNode.getClassTypeStr());
		para.setDescription(validateNode.getDescription());
		para.setJsMethodName(validateNode.getJSMethodName());
		para.setCustemJSMethodInfo(validateNode.getCustemJSMethodInfo());
		basicvaluse.add(para);
		
		
		parameters.put("basicvaluse", basicvaluse);										
		parameters.put("parameters", createParaList(validateNode));						
		parameters.put("errors", createErrorList(validateNode));						
		
		FreemarkerUtils.writeTemplateToFile(tempalatePath, parameters, filePath 
				+ File.separator + filename + ".html");
	}
	
	/**
	 * 创建验证构件参数列表的html文档
	 * 
	 * @param validateNode
	 * @return List<Parameters>
	 */
	private static  List<Parameters> createErrorList(ValidateVO validateNode) {
		List<Parameters> errors = new ArrayList<Parameters>();
		Map<String, String> errorinfos = validateNode.getErrorInfos();
		
		for (String  key : errorinfos.keySet()) {
			Parameters errorlist = new Parameters();
			errorlist.setKey(key);
			errorlist.setErroinfo(errorinfos.get(key));
			errors.add(errorlist);
		}
		
		return errors;
	}
	
	/**
	 * 创建验证错误信息列表的html文档
	 * 
	 * @param validateNode
	 * @return List<Parameters>
	 */
	private static List<Parameters> createParaList(ValidateVO validateNode) {
		List<Parameters> args = new ArrayList<Parameters>();
		List<String> argset = validateNode.getArgs();
		int i = 1;
		for (String string : argset) {
			Parameters arglist = new Parameters();
			arglist.setId(Integer.toString(i++));
			arglist.setParameter(string);
			args.add(arglist);
		}
		return args; 
	}
}
